#include "mainwindow2.h"
#include "ui_mainwindow2.h"
#include <QPainter>
#include <QPaintEvent>
#include <math.h>
#include <QDebug>
#include <QString>
#include <qstring.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include "e.h"
#include <unistd.h>
#include <QTimer>
extern int cost;
extern struct edge E[1000];
extern struct vposition V[100];
extern int e[1000];
extern int n;
extern int m;
extern void prim();
extern void kruskal();
int trigger=0;
extern int flag;
int times;
int mani;
int tbc=-1;
int tstep;
int oneflag;
extern int vertexnum;
#define INTERVAL 5
void MainWindow2::timeschedule()
{
    tstep++;
    if(tstep==INTERVAL+1)
    {
        tstep=0;
        times++;
    }
    if(times==vertexnum-1)
    {
        times=0;
        oneflag=!oneflag;
        ui->tb->clear();
    }
}

MainWindow2::MainWindow2(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow2)
{
    ui->setupUi(this);
}

void MainWindow2::paintEvent(QPaintEvent *event)
{
    QString ch;
    Q_UNUSED(event);
    QPainter painter(this);
    QFont font1("Agency FB",20,QFont::Bold,false);
    painter.setFont(font1);
    if(flag==0 || flag==1){
        for (int i=0;i<m;i+=2 ) {
            painter.setPen(QPen(QColor(0,0,0)));
            painter.drawLine(V[E[i].u].x,V[E[i].u].y,V[E[i].v].x,V[E[i].v].y);
        }
        for(int i=0;i<n;i++)
        {
            ch=QString::number(i);
            painter.setPen(QPen(QColor(0,0,0),10));
            painter.setBrush(QColor(0,0,0));
            painter.drawEllipse(QPointF(V[i].x, V[i].y),20,20);
            painter.setPen(QPen(QColor(255,255,0)));
            painter.drawText(V[i].x-10,V[i].y+10,ch);
        }
        if(trigger==1&&vertexnum!=0)
        {
            update();
            QString ch;
            QFont font1("Agency FB",20,QFont::Bold,false);
            painter.setFont(font1);
            painter.setPen(QPen(QColor(0,255,0),10));
            painter.setBrush(QColor(0,255,0));
            painter.drawEllipse(QPointF(V[E[e[0]].u].x,V[E[e[0]].u].y),20,20);
            painter.setPen(QPen(QColor(255,255,0)));
            ch=QString::number(E[e[0]].u);
            painter.drawText(V[E[e[0]].u].x-10,V[E[e[0]].u].y+10,ch);
            for(int i=0;i<=times;i++){
                if(i<times){
                    painter.setPen(QPen(QColor(0,255,0)));
                    painter.drawLine(V[E[e[i]].u].x,V[E[e[i]].u].y,V[E[e[i]].v].x,V[E[e[i]].v].y);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].u].x,V[E[e[i]].u].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].u);
                    painter.drawText(V[E[e[i]].u].x-10,V[E[e[i]].u].y+10,ch);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].v].x,V[E[e[i]].v].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].v);
                    painter.drawText(V[E[e[i]].v].x-10,V[E[e[i]].v].y+10,ch);
                }
                if(i==times)
                {
                    painter.setPen(QPen(QColor(0,255,0)));
                    painter.drawLine(V[E[e[i]].u].x,V[E[e[i]].u].y,V[E[e[i]].u].x+(V[E[e[i]].v].x-V[E[e[i]].u].x)/INTERVAL*tstep,V[E[e[i]].u].y+(V[E[e[i]].v].y-V[E[e[i]].u].y)/INTERVAL*tstep);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].u].x,V[E[e[i]].u].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].u);
                    painter.drawText(V[E[e[i]].u].x-10,V[E[e[i]].u].y+10,ch);
                    if(tstep==INTERVAL)
                    {
                        painter.setPen(QPen(QColor(0,255,0),10));
                        painter.setBrush(QColor(0,255,0));
                        painter.drawEllipse(QPointF(V[E[e[i]].v].x,V[E[e[i]].v].y),20,20);
                        painter.setPen(QPen(QColor(255,255,0)));
                        ch=QString::number(E[e[i]].v);
                        painter.drawText(V[E[e[i]].v].x-10,V[E[e[i]].v].y+10,ch);
                    }
                }
                update();
            }
            update();
            if(times!=tbc&&vertexnum>2)
            {
                QString ch;
                QString q=("第"+QString::number(times+1)+"步：\n");
                ui->tb->append(q);
                QString q2=(QString::number(E[e[times]].u)+" "+QString::number(E[e[times]].v)+" 权值："+QString::number(E[e[times]].cost)+"\n");
                ui->tb->append(q2);
                if(times==vertexnum-2){
                    QString q3=("总权值："+QString::number(cost)+"\n");
                    ui->tb->append(q3);
                }
                update();
            }
            if(oneflag!=tbc&&vertexnum==2)
            {
                QString ch;
                QString q=("第"+QString::number(times+1)+"步：\n");
                ui->tb->append(q);
                QString q2=(QString::number(E[e[times]].u)+" "+QString::number(E[e[times]].v)+" 权值："+QString::number(E[e[times]].cost)+"\n");
                ui->tb->append(q2);
                if(times==vertexnum-2){
                    QString q3=("总权值："+QString::number(cost)+"\n");
                    ui->tb->append(q3);
                }
                update();
            }
            if(vertexnum>2)
                tbc=times;
            if(vertexnum==2)
                tbc=oneflag;
        }
    }
    if(flag==2 || flag==3){
        for(int i=0;i<m;i+=2)
        {
            if(E[i].cost!=0)
            {
                painter.setPen(QPen(QColor(0,0,0)));
                painter.drawLine(V[E[i].u].x,V[E[i].u].y,V[E[i].v].x,V[E[i].v].y);
                ch=QString::number(E[i].u);
                painter.setPen(QPen(QColor(0,0,0),10));
                painter.setBrush(QColor(0,0,0));
                painter.drawEllipse(QPointF(V[E[i].u].x, V[E[i].u].y),20,20);
                painter.setPen(QPen(QColor(255,255,0)));
                painter.drawText(V[E[i].u].x-10,V[E[i].u].y+10,ch);
                ch=QString::number(E[i].v);
                painter.setPen(QPen(QColor(0,0,0),10));
                painter.setBrush(QColor(0,0,0));
                painter.drawEllipse(QPointF(V[E[i].v].x, V[E[i].v].y),20,20);
                painter.setPen(QPen(QColor(255,255,0)));
                painter.drawText(V[E[i].v].x-10,V[E[i].v].y+10,ch);
            }
            update();
        }
        if(trigger==1&&vertexnum!=0)
        {
            update();
            QString ch;
            QFont font1("Agency FB",20,QFont::Bold,false);
            painter.setFont(font1);
            painter.setPen(QPen(QColor(0,255,0),10));
            painter.setBrush(QColor(0,255,0));
            painter.drawEllipse(QPointF(V[E[e[0]].u].x,V[E[e[0]].u].y),20,20);
            painter.setPen(QPen(QColor(255,255,0)));
            ch=QString::number(E[e[0]].u);
            painter.drawText(V[E[e[0]].u].x-10,V[E[e[0]].u].y+10,ch);
            for(int i=0;i<=times;i++){
                if(i<times){
                    painter.setPen(QPen(QColor(0,255,0)));
                    painter.drawLine(V[E[e[i]].u].x,V[E[e[i]].u].y,V[E[e[i]].v].x,V[E[e[i]].v].y);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].u].x,V[E[e[i]].u].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].u);
                    painter.drawText(V[E[e[i]].u].x-10,V[E[e[i]].u].y+10,ch);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].v].x,V[E[e[i]].v].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].v);
                    painter.drawText(V[E[e[i]].v].x-10,V[E[e[i]].v].y+10,ch);
                }
                if(i==times)
                {
                    painter.setPen(QPen(QColor(0,255,0)));
                    painter.drawLine(V[E[e[i]].u].x,V[E[e[i]].u].y,V[E[e[i]].u].x+(V[E[e[i]].v].x-V[E[e[i]].u].x)/INTERVAL*tstep,V[E[e[i]].u].y+(V[E[e[i]].v].y-V[E[e[i]].u].y)/INTERVAL*tstep);
                    painter.setPen(QPen(QColor(0,255,0),10));
                    painter.setBrush(QColor(0,255,0));
                    painter.drawEllipse(QPointF(V[E[e[i]].u].x,V[E[e[i]].u].y),20,20);
                    painter.setPen(QPen(QColor(255,255,0)));
                    ch=QString::number(E[e[i]].u);
                    painter.drawText(V[E[e[i]].u].x-10,V[E[e[i]].u].y+10,ch);
                    if(tstep==INTERVAL)
                    {
                        painter.setPen(QPen(QColor(0,255,0),10));
                        painter.setBrush(QColor(0,255,0));
                        painter.drawEllipse(QPointF(V[E[e[i]].v].x,V[E[e[i]].v].y),20,20);
                        painter.setPen(QPen(QColor(255,255,0)));
                        ch=QString::number(E[e[i]].v);
                        painter.drawText(V[E[e[i]].v].x-10,V[E[e[i]].v].y+10,ch);
                    }
                }
                update();
            }
            update();
            if(times!=tbc&&vertexnum>2)
            {
                QString ch;
                QString q=("第"+QString::number(times+1)+"步：\n");
                ui->tb->append(q);
                QString q2=(QString::number(E[e[times]].u)+" "+QString::number(E[e[times]].v)+" 权值："+QString::number(E[e[times]].cost)+"\n");
                ui->tb->append(q2);
                if(times==vertexnum-2){
                    QString q3=("总权值："+QString::number(cost)+"\n");
                    ui->tb->append(q3);
                }
                update();
            }
            if(oneflag!=tbc&&vertexnum==2)
            {
                QString ch;
                QString q=("第"+QString::number(times+1)+"步：\n");
                ui->tb->append(q);
                QString q2=(QString::number(E[e[times]].u)+" "+QString::number(E[e[times]].v)+" 权值："+QString::number(E[e[times]].cost)+"\n");
                ui->tb->append(q2);
                if(times==vertexnum-2){
                    QString q3=("总权值："+QString::number(cost)+"\n");
                    ui->tb->append(q3);
                }
                update();
            }
            if(vertexnum>2)
                tbc=times;
            if(vertexnum==2)
                tbc=oneflag;
        }
    }
}


MainWindow2::~MainWindow2()
{
    delete ui;
}


void MainWindow2::on_ps_clicked()
{
    update();
    QTimer* mtime=new QTimer(this);
    connect(mtime,&QTimer::timeout,this,&MainWindow2::timeschedule);
    mtime->start(300);
    if(flag==2)
    {
        prim();
    }
    if(flag==3)
    {
        kruskal();
    }
    if(mani==1)
        this->close();
    if(mani==0)
    {
        trigger=1;
    }
    mani++;
}

